﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
using NLog;
using ThePlaceRuMainForm.Classes;
using ThePlaceRuMainForm.Models;

namespace ThePlaceRuMainForm
{
    public partial class MainForm : Form
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private string FolderToSave { get; set; }
        public MainForm()
        {
            InitializeComponent();
        }
        private void Button1Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(FolderToSave) && !string.IsNullOrEmpty(txtInputLink.Text))
            {
                var downloader = new ThePlaceRuDownloader("ThePlace");
                var celebrity = new Celebrity();
                celebrity.Url = downloader.GetCorrectLink(txtInputLink.Text);
                celebrity.Pages = downloader.GetCelebritiesPagesCount(celebrity);
                celebrity.CountPhoto = downloader.GetCountPhotoCelebrities(celebrity);
                slblValue.Text = string.Format("Всього фотографій: {0}", celebrity.CountPhoto);
                var imagesLink = downloader.GetAllPhotosCelebrity(celebrity);
                
                foreach (Photo image in imagesLink)
                {
                    using (var client = new WebClient())
                    {
                        var pathToSave = FolderToSave + "\\" + image.Name;
                        client.DownloadFile(image.Link, pathToSave);
                        Logger.Trace(string.Format("Download {0}", image.Link));
                    }
                }
            }


        }

        private void AboutToolStripMenuItemClick(object sender, EventArgs e)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            var version = string.Format("Your version: {0}", fvi.ProductVersion);
            var title = string.Format("Version - {0}", fvi.ProductVersion);
            MessageBox.Show(version, title);
        }

        private void BtnSetSaveFolderClick(object sender, EventArgs e)
        {
            if (dialogSelectFolderForSaveImages.ShowDialog(this) == DialogResult.OK)
            {
                FolderToSave = dialogSelectFolderForSaveImages.SelectedPath;
                txtSavePath.Text = FolderToSave;
            }
        }


    }
}
